﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class FieldSpecificationDAL : IFieldSpecificationDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public FieldSpecificationDAL()
        {

        }
        public IList<FieldSpecificationInfo> ListFieldSpecification(FieldSpecificationInfo specInfo)
        {
            string strSQL;
            int fieldSeq = specInfo.Fields == null ? 0 : specInfo.Fields.Sequence;
            int classSeq = specInfo.Class == null ? 0 : specInfo.Class.Sequence;
            List<FieldSpecificationInfo> infoList = new List<FieldSpecificationInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", specInfo.Sequence);
                dbManager.AddParameters(2, "@fieldSeq", fieldSeq);
                dbManager.AddParameters(3, "@classSeq", classSeq);

                strSQL = "SELECT FIELD_SPEC_SEQ, FIELD_POS_SEQ, CLASS_SEQ, REMARKS"
                         + ", RECORD_STATUS, CREATED_BY, UPDATED_BY"
                     + " FROM HRM_MP_FIELD_SPECIFICATIONS"
                     + " WHERE RECORD_STATUS not like @DeleteRecord"
                     + (specInfo.Sequence > 0 ? " AND FIELD_SPEC_SEQ = @Seq" : "")
                     + (fieldSeq > 0 ? " AND FIELD_POS_SEQ = @fieldSeq" : "")
                     + (classSeq > 0 ? " AND CLASS_SEQ = @classSeq" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    FieldDAL fieldDAL = new FieldDAL();
                    FieldInfo fieldInfo = new FieldInfo();
                    if (!rdr.IsDBNull(1))
                    {
                        IList<FieldInfo> fieldList = fieldDAL.ListFields(new FieldInfo(rdr.IsDBNull(1) ? 0 : rdr.GetInt32(1)));
                        fieldInfo = fieldList.Count > 0 ? fieldList[0] : null;
                    }

                    ClassDAL classDAL = new ClassDAL();
                    ClassInfo classInfo = new ClassInfo();
                    if (!rdr.IsDBNull(2))
                    {
                        IList<ClassInfo> classList = classDAL.ListClasses(new ClassInfo(rdr.GetInt32(2)));
                        classInfo = classList.Count > 0 ? classList[0] : null;
                    }

                    FieldSpecificationInfo Info = new FieldSpecificationInfo(rdr.GetInt32(0)
                            , fieldInfo
                            , classInfo
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                            , rdr.IsDBNull(5) ? (int?)null : rdr.GetInt32(5)
                            , rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }

        public void Insert(FieldSpecificationInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@FieldPosSeq", Info.Fields.Sequence);
                dbManager.AddParameters(1, "@ClassSeq", Info.Class.Sequence);
                dbManager.AddParameters(2, "@Remarks", Info.Remarks);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_FIELD_SPECIFICATIONS(FIELD_POS_SEQ, CLASS_SEQ, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@FieldPosSeq, @ClassSeq, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(FieldSpecificationInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Sequence", Info.Sequence);
                dbManager.AddParameters(1, "@FieldPosSeq", Info.Fields.Sequence);
                dbManager.AddParameters(2, "@ClassSeq", Info.Class.Sequence);
                dbManager.AddParameters(3, "@Remarks", Info.Remarks);
                dbManager.AddParameters(4, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_FIELD_SPECIFICATIONS"
                           + " SET FIELD_POS_SEQ = @FieldPosSeq"
                                + ", CLASS_SEQ = @ClassSeq"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE FIELD_SPEC_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_FIELD_SPECIFICATIONS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE FIELD_SPEC_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
